package dao;

import Utils.JDBCUtil;
import pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
    // 新增注册信息
    public int insertOne(User user) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into user(account,password,username,usersex,userage,address,signature) values(?,?,?,?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, user.getAccount());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setString(3, user.getUsername());
            preparedStatement.setString(4, user.getUsersex());
            preparedStatement.setInt(5, user.getUserage());
            preparedStatement.setString(6, user.getAddress());
            preparedStatement.setString(7, user.getSignature());

            count = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(preparedStatement, connection);
        }
        return count;

    }

    //删除账号
    public int deleteOne(String account) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "delete from user where account=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, account);
            count = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(preparedStatement, connection);
        }
        return count;
    }

    // 修改信息
    public int changeOne(User user) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "update user set password=?,username=?,usersex=?,userage=?,address=?,signature=? where account=?";
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, user.getPassword());
            preparedStatement.setString(2, user.getUsername());
            preparedStatement.setString(3, user.getUsersex());
            preparedStatement.setInt(4, user.getUserage());
            preparedStatement.setString(5, user.getAddress());
            preparedStatement.setString(6, user.getSignature());
            preparedStatement.setString(7, user.getAccount());

            count = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(preparedStatement, connection);
        }
        return count;
    }

    //查找账号
    public User findOne(String account) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select * from user where account=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, account);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                user = new User(account, resultSet.getString("password"), resultSet.getString("username"),
                        resultSet.getString("usersex"), resultSet.getInt("userage"),
                        resultSet.getString("address"), resultSet.getString("signature"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(resultSet, preparedStatement, connection);
        }
        return user;
    }
}
